package com.runchinaup.sifitbledome.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.runchinaup.sifitbledome.R;
import com.runchinaup.sifitbledome.ble.BleHelper;
import com.runchinaup.sifitbledome.ble.ScanHelper;
import com.runchinaup.sifitbledome.ble.conn.ConnResult;
import com.runchinaup.sifitbledome.domain.BleData;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * Created by liber on 2018/3/15.
 */

public class ScanActivity extends Activity implements BleHelper.BleListener {

    private ListView listView;
    private TextView centerTitle;
    private TextView rightText;
    private ScanAdapter scanAdapter;
    ArrayList<BleData> data = new ArrayList<>();
    HashSet<String> addData = new HashSet<>();
    private ProgressDialog progressDialog;

    private ScanHelper scanHelper = ScanHelper.getScanHelper(this);
    private BleHelper bleHelper = BleHelper.getBleHelper();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.ui_scan);
        initView();
        initAdapter();

        //注册连接成功的监听
        bleHelper.registerBleListener(this);
    }

    private void initView() {
        listView = findViewById(R.id.listview);
        centerTitle = findViewById(R.id.centerText);
        rightText = findViewById(R.id.rightText);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在连接设备，请等待");

        rightText.setText("重新扫描");
        centerTitle.setText("扫描设备");

    }

    //初始化适配器和适配listView
    private void initAdapter() {
        scanAdapter = new ScanAdapter(this, data) {
            @Override
            protected void onLayoutClick(BleData bleData) {
                scanHelper.stopScan();
                Toast.makeText(ScanActivity.this, "正在连接中", Toast.LENGTH_SHORT).show();
                bleHelper.connDevice(bleData, ScanActivity.this);
                progressDialog.show();
            }
        };
        listView.setAdapter(scanAdapter);
        makeData();
    }

    //添加数据到数组里面
    private void makeData() {
        data.clear();
        addData.clear();
        scanHelper.startScan(new ScanHelper.ScanListener() {
            @Override
            public void onScanCallback(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!addData.contains(device.getAddress())) {
                            Log.e("debug_scan", device.getName());
                            if (device.getName().equals("sifit")) {
                                data.add(new BleData(device.getName(), device.getAddress()));
                                addData.add(device.getAddress());
                                scanAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        });

    }

    //点击刷新
    public void onRightClick(View view) {
        Toast.makeText(this, "正在重新扫描", Toast.LENGTH_SHORT).show();
        makeData();
    }

    //连接状态的判断
    @Override
    public void onConnectResult(final ConnResult connResult) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.cancel();
                if (connResult == ConnResult.CONN_SUCCESS) {
                    Toast.makeText(ScanActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("isConn", true);
                    setResult(222, intent);
                    ScanActivity.this.finish();
                } else if (connResult == ConnResult.CONN_FAILURE) {
                    Toast.makeText(ScanActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    //接受ble发送的数据
    @Override
    public void onRecevieData(byte[] data) {

    }
}
